﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using com.force.json;

namespace Ys4GClient
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }

        private void register_Load(object sender, EventArgs e)
        {
            regist_loading.Visible = false;
            WinFormHint.Win32Utility.SetCueText(this.regist_account, "请输入手机号");
            WinFormHint.Win32Utility.SetCueText(this.regist_password1, "请输入密码");
            WinFormHint.Win32Utility.SetCueText(this.regist_password2, "请确认输入密码");
            WinFormHint.Win32Utility.SetCueText(this.regist_email, "请输入邮箱");
        }

        private void regist_btn_Click(object sender, EventArgs e)
        {
            String account = regist_account.Text.Trim();
            String password1 = regist_password1.Text.Trim();
            String password2 = regist_password2.Text.Trim();
            String email = regist_email.Text.Trim();
            if (account.Length == 0)
            {
                MessageBox.Show("注册手机号不能为空！");
                return;
            }
            if (password1.Length < 6 || password2.Length < 6)
            {
                MessageBox.Show("注册密码必须为6个以上字符！");
                return;
            }
            if (email.Length == 0)
            {
                MessageBox.Show("注册邮箱不能为空！");
                return;
            }
            if (!isTelNumber(account))
            {
                MessageBox.Show("输入的手机号不合法，请检查！");
                return;
            }
            if (password1 != password2)
            {
                MessageBox.Show("两次输入的密码不相同，请确认！");
                return;
            }
            if (!isEmail(email))
            {
                MessageBox.Show("输入的邮箱地址不合法，请检查！");
                return;
            }
            //String json = "{\"username\":\"" + account + "\",\"password\":\"" + password1 + "\",\"passwordRetry\":\"" + password2 + "\",\"email\":\"" + email + "\",\"phone\":" + account + "}";
            JSONObject jsonObj = new JSONObject();
            jsonObj.Put("username", account);
            jsonObj.Put("password", password1);
            jsonObj.Put("passwordRetry", password2);
            jsonObj.Put("email", email);
            jsonObj.Put("phone", long.Parse(account));
            MyHttp myHttp = new MyHttp();
            String ret = myHttp.httpPost(Constant.REGIST_URL, jsonObj.ToString());
            if (ret == null)
            {
                MessageBox.Show("注册失败，请检查网络连接！");
            } 
            else
            {
                JSONObject retObj = new JSONObject(ret);
                int state = retObj.GetInt("state");
                String message = retObj.GetString("message");
                if (state == 1)
                {
                    DialogResult result = MessageBox.Show("注册成功，请登录注册邮箱，激活账户使用！", "注册成功", MessageBoxButtons.OK, MessageBoxIcon.Question);
                    if (result == DialogResult.OK)
                    {
                        login.mInstance.setRegistAccount(account);
                        login.mInstance.Show();
                        this.Close();
                    }
                }
                else if (state == 0)
                {
                    MessageBox.Show("注册失败，" + message);
                }
            }
        }

        private bool isTelNumber(String num)
        {
            return Regex.IsMatch(num, @"^((0?1[358]\d{9})|((0(10|2[1-3]|[3-9]\d{2}))?[1-9]\d{6,7}))$");
        }

        private bool isEmail(string email)
        {
            return Regex.IsMatch(email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }

        private void Register_FormClosing(object sender, FormClosingEventArgs e)
        {
            login.mInstance.Show();
        }
    }
}
